﻿using Sdg.Domain.BizObj;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sdg.Domain;
using System.Data.SqlClient;

namespace SDataGen.Views
{
    /// <summary>
    /// Логика взаимодействия для ConnectionView.xaml
    /// </summary>
    public partial class ConnectionView : BaseView, IConnectionView
    {
        private Connection _connection = null;

        public ConnectionView() :
            base()
        {
            InitializeComponent();
            _connection = new Connection() { ServerName = @".\sqlexpress", DbName = "tp", IntegratedSecurity = true };
            Bind(_connection);        
            OnAfterInit(this, new ViewEventArgs(this, null));
        }

        public override void Bind(IBizObj bizObj)
        {
            base.Bind(bizObj);
            ServerTextBox.SetBinding(TextBox.TextProperty, new Binding("ServerName") { Mode = BindingMode.TwoWay });
            DbTextBox.SetBinding(TextBox.TextProperty, new Binding("DbName") { Mode = BindingMode.TwoWay });
            PasswordTextBox.SetBinding(TextBox.TextProperty, new Binding("Password") { Mode = BindingMode.TwoWay });
            UserTextBox.SetBinding(TextBox.TextProperty, new Binding("UserName") { Mode = BindingMode.TwoWay });
            UseIS.SetBinding(CheckBox.IsCheckedProperty, new Binding("IntegratedSecurity") { Mode = BindingMode.TwoWay });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (CheckConnection())
                {
                    OnDone(this, new ViewEventArgs(this, BizObject));
                   // MessageBox.Show("Тест соединения успешен", "Проверка соединения...", MessageBoxButton.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show( "Ошибка соединения с сервером БД\r\n" + ex.Message, "Проверка соединения...", MessageBoxButton.OK);
            }
        }

        private bool CheckConnection()
        {
            SqlConnection conn = new SqlConnection(_connection.ConnectionString);
            conn.Open();
            conn.Close();

            return true;
        }

        public override void Notify(ViewEventArgs args)
        {
            
        }
    }

    public class BoolInvertorConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
